var key = CryptoJS.enc.Hex.parse("d86a7bab346ac01ad9dc6b897652f2d2");
// 加密
export function Encrypt(word) {
	var srcs = CryptoJS.enc.Utf8.parse(word);
	var encrypted = CryptoJS.AES.encrypt(srcs, key, {
		mode : CryptoJS.mode.ECB,
		padding : CryptoJS.pad.Pkcs7
	});
	return encrypted.toString();
}
// 解密
export function Decrypt(word) {
	var decrypt = CryptoJS.AES.decrypt(word, key, {
		mode : CryptoJS.mode.ECB,
		padding : CryptoJS.pad.Pkcs7
	});
	return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
// 路由跳转
export function jumpRout(_that,path,parameter,run){
  _that.$router.push(
    {
      path:path,
      query:parameter
    }
  )
  run(parameter)
}
//swiper 参数
export function swiperConfig(_that,data,swiperConfig, swiperData,run){
  let fy
  data.fy ? fy = '.'+data.fy : ''
  let datas = {
    slidesPerView:data.view  || 1,
    pagination: {
      el: fy
    },
    //设置点击箭头
    navigation: {
        nextEl: '.'+ data.next || '',
        prevEl: '.'+ data.prev || '',
    },
    autoplay: {
      delay: data.delay || 3000, //多少秒动一次
      disableOnInteraction: data.autoplayLast || false, //当用户滑动图片后继续自动轮播
    },
    loop: data.loop || false, //开启循环模式
    on:{
      slideChangeTransitionEnd:function(){
        run(this.realIndex)
      }
    }
  }
  _that[swiperConfig] = datas //轮播配置
  _that[swiperData] = data  //轮播参数
}
//提示语
export function showToast(_that,msg,type = 0){
  if(type == 0){
    type = 'error'
  }else{
    type = 'success'
  }
  _that.$message({
    showClose: true,
    type:type,
    duration:1500,
    message:msg
  });
}

// 提示语2
export function msg(msg,time = 3000){
  document.getElementById('msg').className="show"
  document.getElementById('msgTxt').innerHTML=msg
  setTimeout(()=>{
    document.getElementById('msg').className="hide"
  },time)
}

export function showToastSuccess(_that,msg){
  _that.$message({
    showClose: true,
    type:'success',
    duration:1500,
    message:msg
  });
}
// 不加密接口
export function ajax(_that,ajaxFunc,data,sucFun,error){
	
  ajaxFunc(data).then((res)=>{
    if(res.data.flag){
      if(res.data.code == 200){
        sucFun(res.data)
      }else{
        showToast(_that,res.data.msg)
        error(res.data)
      }
    }else{
      showToast(_that,_that.$t('common.tips.systemError'))
    }
    
  });
  
}
//传参不加密，返回加密
export function ajaxBackAdd(_that,ajaxFunc,data,sucFun,error){
  ajaxFunc(data).then((res)=>{
    res = JSON.parse(Decrypt(res.data))
    if(res.flag){
      if(res.code == 200){
        sucFun(res)
      }else{
        showToast(_that,res.data.msg)
        error(res)
      }
    }else{
      showToast(_that,_that.$t('common.tips.systemError'))
    }
    
  });
  
}
// 加密接口
export function ajaxAdd(_that,ajaxFunc,data,sucFun,error){
  data = Encrypt(JSON.stringify(data))
  console.log('传参',data)
  ajaxFunc(data).then((res)=>{
    res = JSON.parse(Decrypt(res.data))
    console.log('res',res.flag)
    if(res.flag){
      if(res.code == 200){
        sucFun(res)
      }else if(res.code == 403){
        _that.$router.push('/login')
        _that.$store.dispatch('exit')
      }else{
        showToast(_that,res.msg)
        error(res)
      }
    }else{
      showToast(_that,_that.$t('common.tips.systemError'))
    }
  });
  
}


//判断字符串不为空 （只有空格换行，也视为空字符串）
export function notBlankStr(str) {
  if (str == null || str == undefined || str == '') {
    return false;
  } else {
    var regStr = str.toString();
    regStr = regStr.replace(/ /g, "");
    regStr = regStr.replace(/\r\n/g, "");
    regStr = regStr.replace(/\n/g, "");

    if (regStr.length == 0) {
      return false;
    } else {
      return true;
    }
  }
}

//企业信息改编
export function companyInfo(_that,url,xrData=true){
  //企业信息
  ajaxAdd(_that,url,{},res=>{
    let data = res.data
    console.log('企业信息',data)
    if(data){
      if(xrData){
        let formData = _that.formData
        //产品标签显示
        if(!data.technologyDirectionList){
          formData.technologyDirectionList = _that.formData.technologyDirectionList.concat(_that.single)
        }else{
          for(let j=0;j<data.technologyDirectionList.length;j++){
            let arrys = []
            if(data.technologyDirectionList[j].technologyTag && data.technologyDirectionList[j].technologyTag.length>0){
              let technologyTag = data.technologyDirectionList[j].technologyTag.split(',')
              let technologyTagTxt = data.technologyDirectionList[j].technologyTagTxt.split(',')
              for(let i=0;i<technologyTag.length;i++){
                arrys = arrys.concat({text:technologyTagTxt[i],value:technologyTag[i]})
              }
            }
           
            data.technologyDirectionList[j].technologyTag = arrys
          }
          formData.technologyDirectionList = data.technologyDirectionList
        }
        if(data.industry && data.industry.length>0){
          let arry = []
          let industry = data.industry.split(',')
          let industryTxt = data.industryTxt.split(',')
          formData.industryTxt=data.industryTxt;
          for(let i = 0; i<industry.length; i++){
            _that.industryLabe = arry.concat({text:industryTxt[i],value:industry[i]})
          }
        }else{
          formData.industry='';	//所属行业
          formData.industryTxt='';	//所属行业
        }
        //企业标签显示
        let arry = []
        let enterpriseTag = data.enterpriseTag.split(',')
        let enterpriseTagTxt = data.enterpriseTagTxt.split(',')
        for(let i=0;i<enterpriseTag.length;i++){
          arry = arry.concat({text:enterpriseTagTxt[i],value:enterpriseTag[i]})
        }
        _that.qyLabe = arry
        formData.corporateType=data.corporateType;	//企业类型/企业性质
        formData.enterpriseInfo	=data.enterpriseInfo;	//企业信息/经营范围
        formData.enterpriseName	=data.enterpriseName;	//企业名称
        formData.enterpriseProfile=data.enterpriseProfile;	//企业简介
        formData.enterpriseTag=data.enterpriseTag;	//企业标签
        formData.establishmentDate=data.establishmentDate;	//成立日期
        formData.id=data.id;	//企业信息ID
        
        
        formData.ipcount=data.ipcount;	//知识产权数量
        formData.lawPerson=data.lawPerson;	//法人代表
        for(let i=0;i<data.attachmentList.length;i++){
          if(data.attachmentList[i].belongType == 1){
            formData.logoId = data.attachmentList[i].id
            _that.imgs1 = _that.imgUrl+data.attachmentList[i].path
          }
        }
        formData.registeredCapital=data.registeredCapital;	//注册资本
        formData.staffNumber=data.staffNumber;	//员工人数
        formData.status=data.status;	//是否发起认证 1不发起认证，2发起认证
        formData.uscc=data.uscc;	//统一社会信用代码
        // 企业资质
        if(data.qualificationInformationList.length == 0){
          formData.qualificationInformationList = _that.formData.qualificationInformationList 
        }else{
          formData.qualificationInformationList=data.qualificationInformationList
          data.qualificationInformationList[0].attachment ? _that.imgs2 = _that.imgUrl+data.qualificationInformationList[0].attachment.path : _that.imgs2 = ''
        }
        formData.completionRate = data.completionRate
        formData.lawPerson = data.lawPerson
        formData.address = data.address
        
        _that.formData = formData
        _that.detail = data
        console.log('formData===>',formData)
      }
    }
  })
}

//手机正则校验
let testPhone = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入手机号'));
  } else if (!regs.phone.test(value)) {
    callback(new Error('请输入正确的手机号!'));
  } else {
    callback();
  }
};
let regs={
  phone: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,//电话
}
export const reg={
  phone: testPhone,
  phone1: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
  password:/[^\u4e00-\u9fa5]{6,16}/i,
  ch:/[^\u4e00-\u9fa5]{6,16}/i 
  
}
// 文件上传前校验
export function beforeUpload(file,arr = [],size,runFun){
  const typeList = [...arr];
  const fileName = file.name;
  const extension = fileName.substr(fileName.lastIndexOf(".") + 1);
  const isRT = typeList.includes(extension);
  const typeMsg = '文件格式不对，请检查！'
  const sizeMsg = '文件过大，请重新上传！'
  if (!isRT) {
    window.app.$message.error(typeMsg);
  }else if(file.size > size){
    window.app.$message.error(sizeMsg);
  }else{
    runFun()
  }
  
}

// 是否登录
export function isLoginM(_that,run){
  let Authorization = _that.$store.state.Authorization
  if(!Authorization){
    _that.$confirm('请先登录解锁更多功能~', '无权限操作', {
      confirmButtonText: '立即登录',
      cancelButtonText: '我再看看',
      // type: 'warning'
    }).then(() => {
      _that.$router.push('/login')
    }).catch(() => {
              
    });
  }else{
    run()
  }
}
import { getCurrentInfo, } from '@/services/api.js';
export function getUserInfoData(_that,run){
  ajaxAdd(_that,getCurrentInfo,{},res=>{
    let userInfo = res.data
    if(userInfo.photo){
      if(userInfo.photo.indexOf(_that.$store.state.imgUrl) !=-1){
        userInfo.path = userInfo.photo
      }else{
        userInfo.path = _that.$store.state.imgUrl + userInfo.photo
      }
    }else{
      userInfo.path = ''
    }
    if(res.data.type==0){
      userInfo.name = userInfo.userName || userInfo.loginName
    }else{
      userInfo.name = userInfo.enterpriseName || userInfo.loginName
    }
    _that.userInfo = userInfo
    run(userInfo)
  },err=>{})
}

